package com.threeox.commonlibrary.ui.view.multipagenavigation;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.entity.engine.model.multipage.PageDataMessage;
import com.threeox.commonlibrary.ui.view.inter.engine.multipage.INavigationIndicator;
import com.threeox.commonlibrary.ui.view.inter.engine.multipage.OnNavigationListener;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: CommonViewPagerIndicator
 * 
 * @Description: TODO(自定义类库滑动指示器)
 * 
 * @author 赵屈犇
 * 
 * @date 创建时间: 2017/8/10 18:37
 * 
 * @version 1.0
 * 
 */
public class CommonViewPagerIndicator extends LinearLayout implements INavigationIndicator {

    private final String TAG = this.getClass().getName();
    private final String TAGKEY = "_" + TAG;

    // 指示器的颜色
    private int COLOR_INDICATOR_COLOR;
    // 文字的选中和未选中颜色
    private int COLOR_TEXT_NORMAL, COLOR_TEXT_SELECTED;

    private int mTabWidth;
    private int mTabCount;
    private float mTranslationX;
    private List<TextView> mTextViews;
    private Paint mPaint = new Paint();
    private List<PageDataMessage> mPageDataMessages;
    private OnNavigationListener mOnNavigationListener;

    public CommonViewPagerIndicator(Context context) {
        this(context, null);
    }

    public CommonViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(getResources().getColor(R.color.indicator_bg_color));
        COLOR_INDICATOR_COLOR = getResources().getColor(R.color.indicator_color);
        COLOR_TEXT_NORMAL = getResources().getColor(R.color.indicator_color_text_normal);
        COLOR_TEXT_SELECTED = getResources().getColor(R.color.indicator_color_text_selected);
        mPaint.setColor(COLOR_INDICATOR_COLOR);
        mPaint.setStrokeWidth(6.0F);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        try {
            super.onSizeChanged(w, h, oldw, oldh);
            mTabWidth = w / mTabCount;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public INavigationIndicator setPageDataMessages(List<PageDataMessage> pageDataMessages) {
        this.mPageDataMessages = pageDataMessages;
        mTabCount = mPageDataMessages.size();
        generateTitleView();
        return this;
    }

    @Override
    public PageDataMessage getPageDataMessage(int postion) {
        if (EmptyUtils.isNotEmpty(mPageDataMessages)) {
            return mPageDataMessages.get(postion);
        }
        return null;
    }

    @Override
    public List<PageDataMessage> getPageDataMessage() {
        return mPageDataMessages;
    }

    @Override
    public INavigationIndicator updateMessage(int postion, PageDataMessage message) {
        try {
            mPageDataMessages.set(postion, message);
            mTextViews.get(postion).setText(message.getTitle());
        } catch (Exception e) {
            LogUtils.e(TAG, e.getMessage());
        }
        return this;
    }

    @Override
    public INavigationIndicator onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mTranslationX = getWidth() / mTabCount * (position + positionOffset);
        invalidate();
        if (EmptyUtils.isNotEmpty(mTextViews)) {
            for (int i = 0; i < mTextViews.size(); i++) {
                TextView textView = mTextViews.get(i);
                if (i == position) {
                    textView.setTextColor(COLOR_TEXT_SELECTED);
                } else {
                    textView.setTextColor(COLOR_TEXT_NORMAL);
                }
            }
        }
        return this;
    }

    @Override
    public INavigationIndicator setOnNavigationListener(OnNavigationListener onNavigationListener) {
        this.mOnNavigationListener = onNavigationListener;
        return this;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mTranslationX, getHeight() - 2);
        canvas.drawLine(0, 0, mTabWidth, 0, mPaint);
        canvas.restore();
    }

    private void generateTitleView() {
        if (getChildCount() > 0)
            this.removeAllViews();
        int count = mPageDataMessages.size();
        setWeightSum(count);
        mTextViews = new ArrayList<TextView>();
        for (int i = 0; i < count; i++) {
            PageDataMessage pageDataMessage = mPageDataMessages.get(i);
            TextView textView = new TextView(getContext());
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
            lp.weight = 1;
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(COLOR_TEXT_NORMAL);
            textView.setText(pageDataMessage.getTitle());
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            textView.setLayoutParams(lp);
            final int position = i;
            textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // scroll(position, 0f);
                    if (mOnNavigationListener != null) {
                        mOnNavigationListener.onNavigationClick(position);
                    }
                }
            });
            textView.setOnTouchListener(new OnTouchListener() {
                int count = 0;
                int firClick = 0;
                int secClick = 0;

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (MotionEvent.ACTION_DOWN == event.getAction()) {
                        count++;
                        if (count == 1) {
                            firClick = (int) System.currentTimeMillis();
                        } else if (count == 2) {
                            secClick = (int) System.currentTimeMillis();
                            if (secClick - firClick < 1000) {
                                // 双击事件
                                if (mOnNavigationListener != null) {
                                    mOnNavigationListener.onNavigationDoubleClick(position);
                                }
                            }
                            count = 0;
                            firClick = 0;
                            secClick = 0;
                        }
                    }
                    return false;
                }
            });
            addView(textView);
            mTextViews.add(textView);
        }
    }

}
